package com.stock_t.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.stock_t.entity.StockTrading;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface StockTradingMapper extends BaseMapper<StockTrading> {
    
    @Select("SELECT id, trade_date, trade_day, stock_code, stock_name, total_shares, market_value, profit_loss, break_even_price, current_price, t_profit, created_time, updated_time FROM stock_trading WHERE trade_date = #{tradeDate} AND stock_code = #{stockCode}")
    List<StockTrading> selectByPrams(@Param("tradeDate") Date tradeDate, @Param("stockCode") String stockCode);
    
    // 新增：根据股票编码和名称分组查询
    @Select("SELECT stock_code as stockCode, stock_name as stockName FROM stock_trading GROUP BY stock_code, stock_name order by max(trade_date) desc ")
    List<Map<String, String>> selectStockList();

    /**
     * 根据股票代码查询股票名称
     */
    @Select("SELECT stock_name FROM stock_trading WHERE stock_code = #{stockCode} LIMIT 1")
    String selectStockNameByCode(@Param("stockCode") String stockCode);

    /**
     * 获取最新数据
     */
    @Select("SELECT " +
            "total_shares as totalShares, " +
            "t_profit as tProfit, " +
            "break_even_price as breakEvenPrice, " +
            "current_price as currentPrice, " +
            "profit_loss as profitLoss, " +
            "trade_date as date, " +
            "market_value as marketValue " +
            "FROM stock_trading " +
            "WHERE stock_code = #{stockCode} " +
            "ORDER BY trade_date DESC " +
            "LIMIT 1")
    Map<String, Object> selectLatestData(@Param("stockCode") String stockCode);

    /**
     * 获取保本价分析 - 按总股数分组，获取每个股数对应的最低保本价
     */
    @Select("SELECT total_shares as shares, MIN(break_even_price) as breakEvenPrice " +
            "FROM stock_trading " +
            "WHERE stock_code = #{stockCode} AND break_even_price IS NOT NULL AND total_shares > 0 " +
            "GROUP BY total_shares " +
            "ORDER BY total_shares ASC")
    List<Map<String, Object>> selectBreakEvenPrices(@Param("stockCode") String stockCode);

    /**
     * 获取价格天数映射
     */
    @Select("SELECT CAST(current_price AS CHAR) as price, COUNT(*) as days FROM stock_trading " +
            "WHERE stock_code = #{stockCode} AND current_price IS NOT NULL " +
            "GROUP BY current_price " +
            "ORDER BY current_price ASC")
    List<Map<String, Object>> selectPriceDaysMap(@Param("stockCode") String stockCode);

    /**
     * 计算当前价格的天数
     */
    @Select("SELECT COUNT(DISTINCT DATE(created_time)) FROM stock_trading " +
            "WHERE stock_code = #{stockCode} AND current_price = (" +
            "SELECT current_price FROM stock_trading WHERE stock_code = #{stockCode} " +
            "ORDER BY created_time DESC LIMIT 1)")
    Integer countDaysWithCurrentPrice(@Param("stockCode") String stockCode);

    /**
     * 计算总记录数
     */
    @Select("SELECT COUNT(*) FROM stock_trading WHERE stock_code = #{stockCode}")
    Integer countTotalRecords(@Param("stockCode") String stockCode);

    /**
     * 根据股票代码查询所有交易记录
     */
    @Select("SELECT * FROM stock_trading WHERE stock_code = #{stockCode} ORDER BY create_time DESC")
    List<StockTrading> selectByStockCode(@Param("stockCode") String stockCode);

    /**
     * 获取股数统计信息（最大、最小、当前股数）
     */
    @Select("SELECT " +
            "MAX(total_shares) as maxTotalShares, " +
            "MIN(total_shares) as minTotalShares, " +
            "(SELECT total_shares FROM stock_trading WHERE stock_code = #{stockCode} ORDER BY trade_date DESC, created_time DESC LIMIT 1) as currentTotalShares " +
            "FROM stock_trading " +
            "WHERE stock_code = #{stockCode} AND total_shares IS NOT NULL")
    Map<String, Object> selectShareSummary(@Param("stockCode") String stockCode);
}